﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Forms;
using SH.DataAccess;
using SH.Foundation;
using SH.Model;
using MessageBox = System.Windows.MessageBox;

namespace SH.Data.View.ViewModel
{
    [MetadataType(typeof(ISearch))]
    public class Search : ValidationModel, ISearch
    {
        private MainWindowViewModel Owner;
        private int? id;
        private int? edrpou;
        private int count;

        [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999")]
        [Range(0, 999999)]
        public int? Id
        {
            get { return id; }
            set { id = value == 0 ? null : value; }
        }

        // todo: move Range Decoration to the ISearch
        [Range(10000000, 9999999999)]
        public int? Edrpou
        {
            get { return edrpou; }
            set { edrpou = value == 0 ? null : value; }
        }

        public string Name { get; set; }

        public Region Region { get; set; }

        public DelegateCommand SearchExecute { get; set; }

        public Search(MainWindowViewModel owner)
        {
            Owner = owner;
            Name = "";

            SearchExecute = new DelegateCommand(x => Execute());
        }

        private void Execute()
        {
            var maxCount = 100;

            // Склады, выборка данных
            IQueryable<Storehause> query = null;

            if (Id != null && Id != 0)
            {
                query = from x in Owner.context.Storehauses
                        where x.Id == Id
                        select x;
            }
            else if (Edrpou != null && Edrpou != 0)
            {
                query = from x in Owner.context.Storehauses
                        where x.Edrpou == Edrpou
                        select x;
            }
            else 
            {
                if (Region != null)
                {
                    query = from x in Owner.context.Storehauses
                            where x.Name.Contains(Name) && x.Region.Id == Region.Id
                            select x;
                }
                else
                {
                    query = from x in Owner.context.Storehauses
                            where x.Name.Contains(Name)
                            select x;
                }
            }

            Owner.Storehauses = CollectionViewSource.GetDefaultView(query.ToList().Take(maxCount));
            Count = maxCount < query.Count() ? maxCount : query.Count();
        }

        public int Count
        {
            get { return count; }
            private set 
            {
                if (count != value)
                {
                    count = value;
                    RaisePropertyChanged("Count");
                }
            }
        }
    }
}